package com.zunipe.trainticketsystem.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.zunipe.trainticketsystem.R;
import com.zunipe.trainticketsystem.database.entity.Order;
import com.zunipe.trainticketsystem.database.entity.Train;
import com.zunipe.trainticketsystem.database.entity.User;
import com.zunipe.trainticketsystem.database.handler.OrderHandler;
import com.zunipe.trainticketsystem.database.handler.TrainHandler;
import com.zunipe.trainticketsystem.database.handler.UserHandler;

import java.util.List;

public class OrderListAdapter extends RecyclerView.Adapter<OrderListAdapter.OrderViewHolder> {
    private final OrderHandler orderHandler;
    private final UserHandler userHandler;
    private final TrainHandler trainHandler;
    private Context context;
    private List<Order> orderList;

    public OrderListAdapter() {
        orderHandler = new OrderHandler();
        userHandler = new UserHandler();
        trainHandler = new TrainHandler();

        orderList = orderHandler.getOrderList();
    }

    public void updateUI() {
        orderList = orderHandler.getOrderList();
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public OrderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.order_item_layout, parent, false);
        context = parent.getContext();
        return new OrderViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull OrderViewHolder holder, int position) {
        Order order = orderList.get(position);
        User user = userHandler.getUserById(order.getUserId());
        Train train = trainHandler.getTrainById(order.getTrainId());
        holder.tvUsername.setText(user.getUsername());
        holder.tvPlaceOfStart.setText(train.getPlaceOfStart());
        holder.tvPlaceOfEnd.setText(train.getPlaceOfEnd());
        holder.tvPrice.setText(context.getString(R.string.price, order.getPrice()));
        holder.tvTime.setText(order.getTime());
        holder.tvOrderNumber.setText(context.getString(R.string.order_number, order.getOrderNumber()));
        holder.tvStatus.setText(context.getString(R.string.order_payment_status, order.getStatus()));
    }

    @Override
    public int getItemCount() {
        return orderList.size();
    }

    public static class OrderViewHolder extends RecyclerView.ViewHolder {
        TextView tvUsername;
        TextView tvPlaceOfStart;
        TextView tvPlaceOfEnd;
        TextView tvPrice;
        TextView tvTime;
        TextView tvOrderNumber;
        TextView tvStatus;

        public OrderViewHolder(@NonNull View itemView) {
            super(itemView);
            tvUsername = itemView.findViewById(R.id.tv_username);
            tvPlaceOfStart = itemView.findViewById(R.id.tv_place_of_start);
            tvPlaceOfEnd = itemView.findViewById(R.id.tv_place_of_end);
            tvPrice = itemView.findViewById(R.id.tv_price);
            tvTime = itemView.findViewById(R.id.tv_time);
            tvOrderNumber = itemView.findViewById(R.id.tv_order_number);
            tvStatus = itemView.findViewById(R.id.tv_payment_status);
        }
    }
}
